package com.erp.gather.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.gather.api.domain.ErpProductAtlas;
import com.erp.gather.domain.bo.ErpProductAtlasAddBo;
import com.erp.gather.domain.bo.ErpProductAtlasBo;
import com.erp.gather.domain.bo.ErpProductAtlasTagBo;
import com.erp.gather.domain.vo.ErpProductAtlasTagVo;
import com.erp.gather.domain.vo.ErpProductAtlasVo;
import com.erp.gather.service.IErpProductAtlasService;
import com.erp.gather.service.IErpProductAtlasTagService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 美工图集
 *
 * @author erp
 * @date 2023-03-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/productAtlas")
public class ErpProductAtlasController extends BaseController {

    private final IErpProductAtlasService iErpProductAtlasService;
    private final IErpProductAtlasTagService iErpProductAtlasTagService;


    /**
     * 删除尺寸不符的图片
     * @param response
     */
    @GetMapping("/getErrorImage")
    public void getErrorImage(HttpServletResponse response) {
        List<ErpProductAtlasVo> list = iErpProductAtlasService.getErrorImage();
//        ExcelUtil.exportExcel(list, "美工图集", ErpProductAtlasVo.class, response);
    }

    @PostMapping("/addProductAtlas")
    public R<Void> addProductAtlas(@RequestBody ErpProductAtlasAddBo bo){
        iErpProductAtlasService.addProductAtlas(bo);
        return toAjax(true);
    }

    /**
     * 查询美工图集列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpProductAtlasVo> list(ErpProductAtlasBo bo, PageQuery pageQuery) {
        return iErpProductAtlasService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取美工图集详细信息
     *
     * @param atlasId 主键
     */
    @GetMapping("/{atlasId}")
    public R<ErpProductAtlasVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long atlasId) {
        return R.ok(iErpProductAtlasService.queryById(atlasId));
    }

    /**
     * 新增美工图集
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpProductAtlasBo bo) {
        return toAjax(iErpProductAtlasService.insertByBo(bo));
    }

    /**
     * 修改美工图集
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpProductAtlasBo bo) {
        return toAjax(iErpProductAtlasService.updateByBo(bo));
    }

    /**
     * 删除美工图集
     *
     * @param atlasIds 主键串
     */
    @DeleteMapping("/{atlasIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] atlasIds) {
        ErpProductAtlasVo erpProductAtlas = iErpProductAtlasService.queryById(atlasIds[0]);
        ErpProductAtlasTagVo erpProductAtlasTag = iErpProductAtlasTagService.queryById(erpProductAtlas.getTagId());
        erpProductAtlasTag.setPictureNumber(erpProductAtlasTag.getPictureNumber() - atlasIds.length);
        iErpProductAtlasTagService.updateByBo(BeanUtil.toBean(erpProductAtlasTag, ErpProductAtlasTagBo.class));
        return toAjax(iErpProductAtlasService.deleteWithValidByIds(Arrays.asList(atlasIds), true));
    }

    /**
     * 根据标签获取信息
     *
     * @param tagId 主键
     */
    @GetMapping("/byTagId{tagId}")
    public R<ErpProductAtlas> getByTagId(@NotNull(message = "主键不能为空")
                                        @PathVariable Long tagId) {
        return R.ok(iErpProductAtlasService.queryByTagId(tagId));
    }
}
